package org.oexam.ostudy.sl;

import org.oexam.ostudy.facade.struct.InsertUserDetails;
import org.oexam.ostudy.struct.Administrators;
import org.oexam.ostudy.struct.Students;
import org.oexam.ostudy.struct.Teachers;
import org.oexam.ostudy.struct.Users;
import org.oexam.ostudy.db.UsersAccess;

public class ManageUsers {
	
	// This class shoud be used for all the user manipulations including reading, editing, creating and deleting users.
	
	
	public String createUser(InsertUserDetails userDetails){
		
// 
		//add Users to db
		//add admin,student,teacher to db
		Users addUser = new Users();
		addUser.setUser_name(userDetails.getUserName());
		
		addUser.setPassword(userDetails.getPassword());
		addUser.setActive(true);
		
		String userType = userDetails.getUserType();
		
		if(userType.equals("admin")) {
			addUser.setUser_type(0);
			
			UsersAccess details = new UsersAccess();
			Users enteredUser = details.getUser(addUser); 
			if( enteredUser == null) {
				return "User Insertion Failed ";				
			} 
			
			Administrators admin = new Administrators(); 
			
			admin.setFirst_name(userDetails.getFirstName());
			admin.setLast_name(userDetails.getLastName());
			admin.setAddress(userDetails.getAddress1() + "," + userDetails.getAddress2());
			admin.setUser_id(enteredUser.getUser_id());
			admin.setPhone(userDetails.getPhoneNumber());
			admin.setEmail(userDetails.getEmailId());
			
			if(!details.insertAdministrator(admin, enteredUser)) {
				return "Unable to add Administrator";	
			}
			
		} else if (userType.equals("teacher")) {
            addUser.setUser_type(1);
			
            UsersAccess details = new UsersAccess();
			Users enteredUser = details.getUser(addUser); 
			if( enteredUser == null) {
				return "User Insertion Failed ";				
			} 
			
            Teachers teacher = new Teachers(); 
			
            teacher.setFirst_name(userDetails.getFirstName());
            teacher.setLast_name(userDetails.getLastName());
            teacher.setAddress(userDetails.getAddress1() + "," + userDetails.getAddress2());
            teacher.setUser_id(enteredUser.getUser_id());
            teacher.setPhone(userDetails.getPhoneNumber());
            teacher.setEmail(userDetails.getEmailId());
			
			if(!details.insertTeacher(teacher, enteredUser)) {
				return "Unable to add Teacher";	
			}
		} else if (userType.equals("student")) {
            addUser.setUser_type(2);
			
            UsersAccess details = new UsersAccess();
			Users enteredUser = details.getUser(addUser); 
			if( enteredUser == null) {
				return "User Insertion Failed ";				
			} 
			
            Students student = new Students(); 
			
            student.setFirst_name(userDetails.getFirstName());
            student.setLast_name(userDetails.getLastName());
            student.setAddress(userDetails.getAddress1() + "," + userDetails.getAddress2());
            student.setUser_id(enteredUser.getUser_id());
            student.setPhone(userDetails.getPhoneNumber());
            student.setEmail(userDetails.getEmailId());
            student.setTeacher_responsible(Integer.parseInt(userDetails.getTeacherResponsible()));
			
			if(!details.insertStudent(student, enteredUser)) {
				return "Unable to add Teacher";	
			}
		}
		
		
		return "";
		
	}

}
